package com.shaozi.nc.mq.substance;

import org.apache.log4j.Logger;
import org.json.JSONException;
import org.json.JSONObject;
import com.shaozi.data.mq.business.base.SourceTypeConsts;
import com.shaozi.data.mq.business.bean.MQExchangeData;
import com.shaozi.data.mq.business.bean.MQExchangeRuleBean;
import com.shaozi.data.mq.business.bean.MQSyncItem;
import com.shaozi.data.mq.business.bus.substance.SubstanceHandler;
import com.shaozi.data.mq.business.utils.MqUtil;
import com.shaozi.lib.throwable.SzException;
import com.shaozi.nc.bean.companyInfo.CompanyInfoUpdateBizBean;
import com.shaozi.nc.companyInfo.biz.CompanyInfoChangedBizService;
import com.shaozi.nc.mq.producer.SendIMInfoProducer;
import com.shaozi.utils.common.BeanUtils;
import com.shaozi.utils.common.JsonUtil;

public class CompanyUserInfoSubstance implements SubstanceHandler {
	
	private Logger logger = Logger.getLogger(CompanyUserInfoSubstance.class);

	@SuppressWarnings("rawtypes")
	@Override
	public void substance(MQExchangeData data, JSONObject jsonObj, String destName) throws JSONException, SzException {
		MQExchangeRuleBean ruleData = (MQExchangeRuleBean) data.getRule();

		if (ruleData.getSync() == null) {
			return;
		}
		MQSyncItem syncItem = ruleData.getSync();
		int baseType = MqUtil.getBaseSourceType(syncItem.getSourceType());
		switch (baseType) {
		case SourceTypeConsts.Sync.CompanyUserInfo.Base:
			exchangeCompanyUserInfoChange(data, jsonObj, syncItem);
			break;
		default:
			break;
		}
	}

	@SuppressWarnings("rawtypes")
	private void exchangeCompanyUserInfoChange(MQExchangeData data, JSONObject jsonObj, MQSyncItem syncItem)
			throws JSONException, SzException {
		switch (syncItem.getSourceType()) {
		case SourceTypeConsts.Sync.CompanyUserInfo.CompanyUserInfoUpdate:
			exchangeCompanyUserInfoChange(data, jsonObj);
			break;
		case SourceTypeConsts.Sync.CompanyUserInfo.CompanyInfoAdd:
			exchangeCompanyInfoAdd(data, jsonObj);
			break;
		default:
			break;
		}
	}

	private void exchangeCompanyUserInfoChange(MQExchangeData data, JSONObject jsonObj)
			throws JSONException, SzException {
		CompanyInfoUpdateBizBean bizBean = JsonUtil.toObject(jsonObj.getJSONObject("rule").get("biz").toString(),
				CompanyInfoUpdateBizBean.class);

		long[] identity = bizBean.getIdentity();
		if (identity != null && identity[0] == 1) {
			// 如果需要更新用户信息，在这里做更新，更新完之后会向IM服务推送MQ消息
			CompanyInfoChangedBizService biz = (CompanyInfoChangedBizService) BeanUtils
					.getBean(CompanyInfoChangedBizService.class);

			biz.processCompanyInfoChange(bizBean.getCompanyId());
		}

		// MQ直接发送到IM
		SendIMInfoProducer producer = (SendIMInfoProducer) BeanUtils.getBean(SendIMInfoProducer.class);
		producer.push(data);
	}

	private void exchangeCompanyInfoAdd(MQExchangeData data, JSONObject jsonObj) {
		String companyId = jsonObj.getJSONObject("rule").getJSONObject("biz").getString("companyId");
		CompanyInfoChangedBizService bizService = (CompanyInfoChangedBizService) BeanUtils
				.getBean(CompanyInfoChangedBizService.class);
		bizService.processCompanyInfoChange(companyId);
	}
}
